package com.example.product_practices.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.product_practices.domain.Question;
import com.example.product_practices.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.altitude.cms.common.entity.AjaxResult;
import xin.altitude.cms.common.entity.PageEntity;

import java.util.Arrays;

@RestController
@CrossOrigin
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private IQuestionService questionService;

    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity, Question question) {
        return AjaxResult.success(questionService.page(pageEntity.toPage(), Wrappers.lambdaQuery(question)));
    }

    @GetMapping("/list")
    public AjaxResult list(Question question) {
        return AjaxResult.success(questionService.list(Wrappers.lambdaQuery(question)));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody Question question) {
        return AjaxResult.success(questionService.save(question));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Question question) {
        return AjaxResult.success(questionService.updateById(question));
    }

    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(questionService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(questionService.getById(id));
    }
}
